package com.jsoft.dao.impl;

import com.jsoft.dao.VoucherDao;
import com.jsoft.entity.Voucher;
import com.jsoft.util.CheckNo;
import com.jsoft.util.DAO;

import java.util.List;
import java.util.Random;

/**
 * @author:sha
 * @email:o68o4741@163.com
 * @date:2023/2/20
 */
public class VoucherDaoImpl extends DAO<Voucher> implements VoucherDao {
    @Override
    public Voucher getVoucher(Integer id) throws Exception {
        String sql="select id,`no`,amount,enterprise_id enterpriseId,open_enterprise_id openEnterpriseId,instituty_id institutyId,open_time openTime,expiry_time expiryTime,uplink_address upLinkAddress,source,`status`,source_id sourceId,payment_interest_type paymentInterestType,remark FROM voucher where id = ?";
        return get(sql,id);
    }

    @Override
    public int updateStatus(Integer id) throws Exception {
        String sql = "UPDATE voucher SET status = 'B' where id=?";
        return update(sql, id);
    }

    @Override
    public List<Voucher> listVoucherNo() throws Exception {
        String sql = "select no from voucher order by no desc";
        return getForList(sql);
    }

    @Override
    public int insertVoucher(Voucher voucher, Integer sourceId, Double balance) throws Exception {
        String vouNo = CheckNo.getVouNo();
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuilder sb=new StringBuilder();
        String stringUpLink=null;
        for(int i=0;i<16;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
            stringUpLink = sb.toString();
        }
//        System.out.println(sb);
        String sql = "INSERT INTO voucher(`no`,amount,enterprise_id,open_enterprise_id,instituty_id,open_time,expiry_time,uplink_address,source,`status`,source_id,payment_interest_type,remark) VALUES(?,?,?,?,?,?,?,?,'B','A',?,?,'这是voucher表中数据的备注')";
        return update(sql,vouNo,balance,voucher.getEnterpriseId(),voucher.getOpenEnterpriseId(),voucher.getInstitutyId(),voucher.getOpenTime(),voucher.getExpiryTime(),stringUpLink,sourceId,voucher.getPaymentInterestType());
    }
}
